package com.songlanyun.modules.order.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.songlanyun.common.enums.*;
import com.songlanyun.common.exception.RRException;
import com.songlanyun.common.utils.OrderGen;
import com.songlanyun.common.utils.StrUtil;
import com.songlanyun.common.validator.ValidatorUtils;
import com.songlanyun.modules.comment.model.dto.OrderGoodsCommentDTO;
import com.songlanyun.modules.comment.service.OrderGoodsCommentService;
import com.songlanyun.modules.exception.MemberException;
import com.songlanyun.modules.exception.OrderException;
import com.songlanyun.modules.gains.service.ShopOrderGainsService;
import com.songlanyun.modules.integralrecord.entity.IntegralRecord;
import com.songlanyun.modules.integralrecord.service.IntegralRecordService;
import com.songlanyun.modules.loverecord.service.UserLoveRecordService;
import com.songlanyun.modules.order.entity.ChildOrderServiceItem;
import com.songlanyun.modules.order.entity.Order;
import com.songlanyun.modules.order.entity.SaleServiceGoodsCard;
import com.songlanyun.modules.order.model.vo.ChildOrderSlimVO;
import com.songlanyun.modules.order.model.vo.ChildOrderVO;
import com.songlanyun.modules.order.model.vo.SubmitChildOrderVo;
import com.songlanyun.modules.order.service.ChildOrderServiceItemService;
import com.songlanyun.modules.order.service.OrderService;
import com.songlanyun.modules.order.service.SaleServiceGoodsCardService;
import com.songlanyun.modules.publicbenefitmsg.model.BuyMsg;
import com.songlanyun.modules.publicbenefitmsg.model.to.PublicBenefitMsgTO;
import com.songlanyun.modules.publicbenefitmsg.service.PublicBenefitMsgService;
import com.songlanyun.modules.saleconfig.entity.SaleConfig;
import com.songlanyun.modules.saleconfig.service.SaleConfigService;
import com.songlanyun.modules.servicegoods.entity.ServiceItem;
import com.songlanyun.modules.shareconfig.entity.SystemShareConfig;
import com.songlanyun.modules.shareconfig.service.SystemShareConfigService;
import com.songlanyun.modules.sharerecord.entity.SystemShareOpenRecord;
import com.songlanyun.modules.sharerecord.service.SystemShareOpenRecordService;
import com.songlanyun.modules.singlegoods.model.OrderStockTo;
import com.songlanyun.modules.singlegoods.service.ProductSingleGoodsService;
import com.songlanyun.modules.singlegoods.service.ProductSingleGoodsStockRecordService;
import com.songlanyun.modules.user.exception.UserException;
import com.songlanyun.modules.usercart.service.UserCartService;
import com.songlanyun.modules.usergains.service.UserBillService;
import org.apache.commons.lang.StringUtils;
import org.redisson.api.RSemaphore;
import org.redisson.api.RedissonClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.math.BigInteger;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.songlanyun.common.utils.PageUtils;

import com.songlanyun.modules.order.dao.ChildOrderDao;
import com.songlanyun.modules.order.entity.ChildOrder;
import com.songlanyun.modules.order.service.ChildOrderService;
import org.springframework.transaction.annotation.Transactional;

/**
 * 订单中的子订单服务
 */
@Service("childOrderService")
public class ChildOrderServiceImpl extends ServiceImpl<ChildOrderDao, ChildOrder> implements ChildOrderService {


    @Autowired
    private ProductSingleGoodsService productSingleGoodsService;

    @Autowired
    private UserCartService userCartService;

    @Autowired
    private ShopOrderGainsService shopOrderGainsService;

    @Autowired
    private OrderService orderService;

    @Autowired
    private SaleConfigService saleConfigService;

    @Autowired
    private OrderGoodsCommentService commentService;

    @Autowired
    private ChildOrderServiceItemService childOrderServiceItemService;

    @Autowired
    private SaleServiceGoodsCardService saleServiceGoodsCardService;

    @Autowired
    private IntegralRecordService integralRecordService;

    @Autowired
    private SystemShareConfigService systemShareConfigService;

    @Autowired
    private SystemShareOpenRecordService systemShareOpenRecordService;

    @Autowired
    private UserLoveRecordService userLoveRecordService;

    @Autowired
    private PublicBenefitMsgService publicBenefitMsgService;

    @Autowired
    private UserBillService userBillService;
    /**
     * 创建子订单
     *
     * @param order
     * @param childOrderVo
     * @return
     */
    @Override
    public ChildOrder create(Order order, SubmitChildOrderVo childOrderVo) {

        ChildOrder childOrder = new ChildOrder();
        childOrder.setOrderType(order.getOrderType());
        childOrder.setShopId(order.getShopId());
        childOrder.setSourceShopId(order.getSourceShopId());
        childOrder.setSourceShopName(order.getSourceShopName());
        childOrder.setShipmentMode(order.getShipmentMode());
        childOrder.setPayOrderCode(order.getPayOrderCode());
        childOrder.setPayOrderId(order.getPayOrderId());
        childOrder.setUserId(order.getUserId());
        childOrder.setOrderId(order.getId());
        childOrder.setOrderCode(order.getOrderCode());
        childOrder.setGoodsId(childOrderVo.getGoodsId());
        childOrder.setGoodsTitle(childOrderVo.getGoodsTitle());
        childOrder.setShortGoodsTitle(childOrderVo.getGoodsShortTitle());
        childOrder.setGoodsImg(childOrderVo.getGoodsCover());
        childOrder.setMarketingType(order.getMarketingType());
        childOrder.setProxyGoods(childOrderVo.getProxyGoods());
        childOrder.setGiftType(childOrderVo.getGiftType());

        childOrder.setActivityId(order.getActivityId());
        childOrder.setActivityTitle(order.getActivityTitle());
        childOrder.setSponsorActivityId(order.getSponsorActivityId());
        childOrder.setActivityGoodsId(childOrderVo.getActivityGoodsId());
        childOrder.setSkuBarCode(childOrderVo.getSkuBarCode());
        childOrder.setSkuHashCode(childOrderVo.getSkuHashCode());
        childOrder.setSkuSetName(childOrderVo.getSkuSetName());
        childOrder.setSingleGoodsId(childOrderVo.getSingleGoodsId());
        childOrder.setSingleGoodsCode(childOrderVo.getSingleGoodsCode());
        childOrder.setSingleGoodsTitle(childOrderVo.getSingleGoodsTitle());
        childOrder.setNumber(childOrderVo.getNum());
        childOrder.setReceiptedNum(0);
        childOrder.setShipNum(0);
        childOrder.setTransactionStatus(OrderConstant.TransactionStatus.WAIT_PAYMENT);
        childOrder.setPaymentStatus(OrderConstant.PaymentStatus.NO_PAY);

        childOrder.setShipStatus(OrderConstant.ShipStatus.NO_SHIP);
        childOrder.setReceiptStatus(OrderConstant.ReceiptStatus.NO);
        childOrder.setCommentStatus(OrderConstant.CommentStatus.NO);
        childOrder.setSelfTakeOperate(false);
        childOrder.setUnit(childOrderVo.getUnit());
        childOrder.setRealPrice(childOrderVo.getFactPrice());
        childOrder.setOriginalRealPrice(childOrderVo.getFactPrice());
        childOrder.setSalePrice(childOrderVo.getSalePrice());
        childOrder.setTotalRealPrice(childOrderVo.getTotalPrice());
        childOrder.setOriginalGoodsAmount(childOrderVo.getOriginalGoodsAmount());
        childOrder.setTotalReduced(childOrderVo.getTotalReduced());
        childOrder.setFirstOrderReduced(childOrderVo.getFirstOrderReduced());
        childOrder.setCost(childOrderVo.getCost());
        childOrder.setTotalCost(childOrderVo.getTotalCost());
        childOrder.setWeight(childOrderVo.getWeight());
        childOrder.setTotalWeight(childOrderVo.getTotalWeight());
        childOrder.setTotalVolume(childOrderVo.getTotalVolume());
        childOrder.setVolume(childOrderVo.getTotalVolume());
        //不做运费分摊
        childOrder.setShouldFreight(BigDecimal.ZERO);

        childOrder.setExchangePoint(childOrderVo.getExchangePoint());
        childOrder.setTotalPoint(childOrderVo.getTotalPoint());
        childOrder.setExchange(order.getExchange());

        //赠送积分
        childOrder.setGiveBackPoint(childOrderVo.getGiveBackPoint());
        childOrder.setTotalGiveBackPoint(childOrderVo.getTotalGiveBackPoint());

        //赠送贡献值
        childOrder.setConsumptionValue(childOrderVo.getConsumptionValue());
        childOrder.setTotalConsumptionValue(childOrderVo.getTotalConsumptionValue());


        childOrder.setShipTime(0L);
        childOrder.setFactReceiptTime(0L);
        childOrder.setLastEndTime(0L);
        childOrder.setLastCommentTime(0L);
        childOrder.setFactCommentTime(0L);

        childOrder.setStockDeductMode(OrderConstant.StockDeductMode.ORDER);
        childOrder.setMobile(order.getMobile());
        childOrder.setProxy(order.getProxy());
        childOrder.setEnd(order.getEnd());
        childOrder.setRightsType(OrderConstant.RightsType.NO);
        childOrder.setRightsStatus(OrderConstant.RightsStatus.NO);
        childOrder.setFirstCategoryId(childOrderVo.getFirstCategoryId());
        childOrder.setGoodsCategoryId(childOrderVo.getGoodsCategoryId());
        /**
         * 如果是消费订单，分析分享提成的提成会员
         */
        childOrder.setShareUserId(getShareUserId(order,childOrder));


        childOrder.setShopCouponSharing(childOrderVo.getShopCouponSharing());

        childOrder.setPlatformCouponSharing(childOrderVo.getPlatformCouponSharing());
        childOrder.setPlatformPointZxSharing(childOrderVo.getPlatformPointZxSharing());
        childOrder.setAreaFeature(childOrderVo.getAreaFeature());
        childOrder.setThird(childOrderVo.getThird());
        childOrder.setThirdSkuId(childOrderVo.getThirdSkuId());
        childOrder.setThirdSpuId(childOrderVo.getThirdSpuId());
        childOrder.setThirdHouniaoSpuId(childOrderVo.getHouniaoSpuId());
        childOrder.setSource(childOrderVo.getSource());

        this.create(childOrder);
        /**
         * 如果是服务订单需要同步服务商品中的 有效天数 免费福利项目 服务次数 提前预约天数
         * 如果是服务订单需要同步服务商品中的服务项目数据
         */
        if(OrderConstant.OrderType.SERVICE_ORDER.equals(childOrder.getOrderType())) {

            createServiceGoodsCard(childOrderVo,childOrder);
            branchSaveServiceItems(childOrder.getId(),childOrderVo.getServiceItems());

        }

        if (childOrderVo.getUserCartId() != null && childOrderVo.getUserCartId() > 0) {
            userCartService.remove(childOrderVo.getUserCartId());
        }

        return childOrder;
    }


    private Long getShareUserId(Order order,ChildOrder childOrder)
    {
        if(OrderConstant.OrderType.SALE_ORDER.equals(order.getOrderType()) && !order.getExchange()
        && order.getMarketingType()==0)
        {
            SystemShareConfig systemShareConfig = systemShareConfigService.loadByShopId(order.getShopId());
            if(systemShareConfig!=null && systemShareConfig.getIsShare())
            {


                if((OrderConstant.ChannelType.WE_APPLET.equals(order.getChannelType()) && systemShareConfig.getIsAppletsShare())
                        ||(OrderConstant.ChannelType.APP.equals(order.getChannelType()) && systemShareConfig.getIsAppShare()))
                {
                    String client = "system";
                    switch (order.getChannelType())
                    {
                        case APP:
                            client = "app";
                            break;
                        case WE_APPLET:
                            client = "applet";
                            break;
                        case SYSTEM:
                            break;
                    }
                    SystemShareOpenRecord systemShareOpenRecord = systemShareOpenRecordService.loadBy(order.getUserId(), childOrder.getGoodsId(), client);
                    if(systemShareOpenRecord!=null)
                    {
                        return systemShareOpenRecord.getShareUserId();
                    }

                }
            }
        }
        return 0L;
    }

    private void createServiceGoodsCard(SubmitChildOrderVo childOrderVo,ChildOrder childOrder)
    {
        SaleServiceGoodsCard saleServiceGoodsCard = new SaleServiceGoodsCard();
        saleServiceGoodsCard.setAdvanceReservation(childOrderVo.getAdvanceReservation());
        saleServiceGoodsCard.setValidity(childOrderVo.getValidity());
        saleServiceGoodsCard.setWelfareItems(childOrderVo.getWelfareItems());
        saleServiceGoodsCard.setServiceCount(childOrderVo.getServiceCount());
        saleServiceGoodsCard.setChildOrderId(childOrder.getId());
        saleServiceGoodsCard.setOrderId(childOrder.getOrderId());
        saleServiceGoodsCard.setUseCount(0);
        saleServiceGoodsCard.setVerificationCode(OrderGen.generateCheckNo());
        saleServiceGoodsCard.setLastCheckTime(null);
        saleServiceGoodsCard.setFirstCheckTime(null);
        saleServiceGoodsCard.setExpiredUseTime(0L);
        saleServiceGoodsCardService.create(saleServiceGoodsCard);
    }

    private void branchSaveServiceItems(Long childOrderId,List<ServiceItem> serviceItems)
    {
        List<ChildOrderServiceItem> collect = serviceItems.stream().map(o -> {
            ChildOrderServiceItem childOrderServiceItem = new ChildOrderServiceItem();
            childOrderServiceItem.setChildOrderId(childOrderId);
            childOrderServiceItem.setItemName(o.getItemName());
            childOrderServiceItem.setItemPrice(o.getItemPrice());
            childOrderServiceItem.setNum(o.getNum());
            return childOrderServiceItem;
        }).collect(Collectors.toList());


        childOrderServiceItemService.branchCreate(childOrderId,collect);
    }



    @Override
    public ChildOrder loadByOrderAndChildId(Long orderId, Long id, boolean tw) {
        ChildOrder one = this.getOne(new LambdaQueryWrapper<ChildOrder>()
                .eq(ChildOrder::getOrderId, orderId)
                .eq(ChildOrder::getId, id)
                .last("limit 1"));
        if (one == null && tw) {
            throw new RRException(OrderException.CHILD_ORDER_NOT_EXISTS);
        }
        return one;
    }

    @Override
    public ChildOrder loadByOrderAndThirdSkuId(Long orderId, Long thirdSkuId, boolean tw) {
        ChildOrder one = this.getOne(new LambdaQueryWrapper<ChildOrder>()
                .eq(ChildOrder::getOrderId, orderId)
                .eq(ChildOrder::getThirdSkuId, thirdSkuId)
                .last("limit 1"));
        if (one == null && tw) {
            throw new RRException(OrderException.CHILD_ORDER_NOT_EXISTS);
        }
        return one;
    }


    /**
     * 子订单退款后封单（TODO 子单封单后可再次计算商家所得和分佣金）
     *
     * @param childOrder
     * @return
     */
    @Override
    public ChildOrder refundOperate(ChildOrder childOrder) {
        childOrder.setRightsStatus(OrderConstant.RightsStatus.COMPLETE);
        childOrder.setEnd(true);
        childOrder.setTransactionStatus(OrderConstant.TransactionStatus.CLOSE);
        this.modify(childOrder);

        return childOrder;
    }

    @Override
    public Integer getBuyGoodsCount(Long userId, Long goodsId, Integer marketingType) {
        return this.baseMapper.getBuyGoodsCount(userId, goodsId, marketingType);
    }

    @Override
    public List<ChildOrder> needAutoEndOrder() {
        return this.list(new LambdaQueryWrapper<ChildOrder>()
                .eq(ChildOrder::getReceiptStatus, OrderConstant.ReceiptStatus.COMPLETE.getCode())
                .eq(ChildOrder::getRightsStatus, OrderConstant.RightsStatus.NO.getCode())
                .eq(ChildOrder::getEnd, false)
                .gt(ChildOrder::getLastEndTime, 0L)
                .le(ChildOrder::getLastEndTime, System.currentTimeMillis())
                .eq(ChildOrder::getTransactionStatus, OrderConstant.TransactionStatus.COMPLETE.getCode()));
    }

    @Override
    public List<ChildOrder> needAutoCommentOrder() {
        return this.list(new LambdaQueryWrapper<ChildOrder>()
                .eq(ChildOrder::getReceiptStatus, OrderConstant.ReceiptStatus.COMPLETE.getCode())
                .eq(ChildOrder::getRightsStatus, OrderConstant.RightsStatus.NO.getCode())
                .eq(ChildOrder::getCommentStatus, OrderConstant.CommentStatus.NO.getCode())
                .gt(ChildOrder::getLastCommentTime, 0L)
                .eq(ChildOrder::getEnd, 0)
        );
    }

    @Override
    public List<ChildOrder> selectNotResultChildOrder(){
//        return this.list(new LambdaQueryWrapper<ChildOrder>()
//                .eq(ChildOrder::getShopId, 9)
//                .in(ChildOrder::getTransactionStatus, "1,3,5")
//                .eq(ChildOrder::getPaymentStatus, 5)
//                .gt(ChildOrder::getCreateTime, "2024-05-14 15:00:00")
//                .eq(ChildOrder::getAreaFeature, 1)
//        );
        return this.baseMapper.selectNotResultOrder();
    }

    /**
     * 自动评论
     *
     * @param childOrder
     */
    @Override
    @Transactional
    public void autoCommentChildOrder(ChildOrder childOrder) {
        //交易没有完成不能评论
        if(!OrderConstant.TransactionStatus.COMPLETE.equals(childOrder.getTransactionStatus()))
        {
            throw new RRException(OrderException.COMPLETE_AFTER_ABLE_COMMENT);
        }
        //已评论不自动评论
        if(OrderConstant.CommentStatus.COMPLETE.equals(childOrder.getCommentStatus()))
        {
            throw new RRException(OrderException.COMMENT_EXISTS);
        }

        SaleConfig config = saleConfigService.getConfig();
        if (config != null && config.getCommentOpen()) {
            OrderGoodsCommentDTO dto = new OrderGoodsCommentDTO();
            dto.setUserId(childOrder.getUserId());
            dto.setChlidOrderId(childOrder.getId());
            dto.setOrderId(childOrder.getOrderId());
            dto.setContent(StringUtils.isNotBlank(config.getDefaultComment()) ? config.getDefaultComment() : "用户超时未评，系统自动好评");
            dto.setLevel(5);
            dto.setShopId(childOrder.getShopId());
            CommentConstant.CommentType type  = CommentConstant.CommentType.GENERAL;
            if(OrderConstant.OrderType.SERVICE_ORDER.equals(childOrder.getOrderType()))
            {
                type =CommentConstant.CommentType.SERVICE;
            }
            dto.setType(type.getCode());
            dto.setGoodsId(childOrder.getGoodsId());
            commentService.saveComment(dto);
            childOrder.setCommentStatus(OrderConstant.CommentStatus.COMPLETE);
            childOrder.setFactCommentTime(System.currentTimeMillis());
            this.modify(childOrder);

            commentChildOrderSync(childOrder);

        }

    }

    /**
     * 评论
     *
     * @param dto
     */
    @Override
    @Transactional
    public void commentChildOrder(OrderGoodsCommentDTO dto) {

        ChildOrder childOrder = this.loadByOrderAndChildId(dto.getOrderId(), dto.getChlidOrderId(), true);
        //交易没有完成不能评论
        if(!OrderConstant.TransactionStatus.COMPLETE.equals(childOrder.getTransactionStatus()))
        {
            throw new RRException(OrderException.COMPLETE_AFTER_ABLE_COMMENT);
        }
        //已评论不自动评论
        if(OrderConstant.CommentStatus.COMPLETE.equals(childOrder.getCommentStatus()))
        {
            throw new RRException(OrderException.COMMENT_EXISTS);
        }
        //不是子订单的会员，不能评论
        if(!dto.getUserId().equals(childOrder.getUserId()))
        {
            throw new RRException(MemberException.USER_EXCEPTION);
        }
        dto.setShopId(childOrder.getShopId());
        dto.setGoodsId(childOrder.getGoodsId());
        //保存评论
        commentService.saveComment(dto);
        childOrder.setCommentStatus(OrderConstant.CommentStatus.COMPLETE);
        childOrder.setFactCommentTime(System.currentTimeMillis());
        this.modify(childOrder);

        commentChildOrderSync(childOrder);

    }

    /**
     * 封闭订单封单
     * @param childOrder
     */
    @Transactional
    @Override
    public void endChildOrder(ChildOrder childOrder)
    {
        if (childOrder.getEnd()) {
            log.error("子订单已封单,不要重复操作");
            return ;
        }

        if(!OrderConstant.TransactionStatus.COMPLETE.equals(childOrder.getTransactionStatus()))
        {
            log.error("子订单交易没有完成不能封单");
            return ;
            //throw new RRException("子订单交易没有完成不能封单");
        }

        if (!OrderConstant.RightsStatus.NO.equals(childOrder.getRightsStatus())) {
            log.error("子订单有维权暂时不能封单");
            return;
            //throw new RRException("子订单有维权暂时不能封单");
        }

        childOrder.setEnd(true);
        this.modify(childOrder);
        endChildOrderSync(childOrder);

        if(childOrder.getTotalGiveBackPoint().compareTo(BigDecimal.ZERO)>0)
        {
            integralRecordService.inner(new IntegralRecord(childOrder.getUserId(),childOrder.getTotalGiveBackPoint(),
                    IntegralConstant.InnerMode.BUY_GOODS, IntegralConstant.IntegralModelType.ORDER,childOrder.getOrderId(),
                    String.format("购买商品【%s】%d %s,订单[%s]完成,赠送积分:%s",childOrder.getGoodsTitle(),childOrder.getNumber(),childOrder.getUnit(),childOrder.getOrderCode(),childOrder.getTotalGiveBackPoint())));
        }

        if(BigDecimal.ZERO.compareTo(childOrder.getTotalConsumptionValue())<0)
        {

            if(GoodsConstant.AreaFeature.GENERAL.equals(childOrder.getAreaFeature()) && childOrder.getTotalConsumptionValue().compareTo(BigDecimal.ZERO)>0)
            {
                userBillService.inBill(childOrder.getUserId(),
                        String.format("购买了\" %s\"",StrUtil.truncation(childOrder.getGoodsTitle(),10)),
                        UserBillConstant.InnerMode.BUY_INCOME,
                        childOrder.getTotalConsumptionValue(),
                        childOrder.getOrderCode(),"系统");
            }
        }


        //结算商家所得和分佣一二级所得
        if(GoodsConstant.AreaFeature.GENERAL.equals(childOrder.getAreaFeature())) {
            shopOrderGainsService.settlementByChildOrder(childOrder);
        }

    }

    /**
     * 同步把订单封单
     * @param childOrder
     */
    @Override
    public void endChildOrderSync(ChildOrder childOrder) {
        Order order = orderService.getById(childOrder.getOrderId());
        if (order != null) {
            boolean isAbleEnd = true;
            List<ChildOrder> childOrderList = order.getChildOrderList(true);
            for (ChildOrder child : childOrderList) {
                if (!childOrder.equals(child) && !child.getEnd()) {
                    isAbleEnd = false;
                    break;
                }
            }
            if (isAbleEnd) {
                order.setEnd(true);
                orderService.modify(order);
            }
        }
    }

    @Override
    public void commentChildOrderSync(ChildOrder childOrder) {
        Order order = orderService.getById(childOrder.getOrderId());
        if (order != null) {
            boolean isAble = true;
            List<ChildOrder> childOrderList = order.getChildOrderList(true);
            for (ChildOrder child : childOrderList) {
                if (!childOrder.equals(child) && !OrderConstant.CommentStatus.COMPLETE.equals(child.getCommentStatus())) {
                    isAble = false;
                    break;
                }
            }
            if (isAble) {
                order.setCommentStatus(OrderConstant.CommentStatus.COMPLETE);
                orderService.modify(order);
            }
        }
    }

    @Override
    public List<ChildOrderVO> vosByOrderId(Long id) {
        return this.baseMapper.vosByOrderId(id);
    }

    @Override
    public List<ChildOrderSlimVO> vosSlimByOrderId(Long id) {
        return this.baseMapper.vosSlimByOrderId(id);
    }


    @Override
    public void processBatchSingleGoodsStock(List<ChildOrder> childOrderList)
    {
        childOrderList.forEach(this::processSingleGoodsStock);
    }



    private void processSingleGoodsStock(ChildOrder childOrder) {
        OrderStockTo orderStockTo = new OrderStockTo();
        orderStockTo.setChangeStock(childOrder.getNumber());
        orderStockTo.setChildReferId(childOrder.getId());
        orderStockTo.setGoodsTitle(childOrder.getGoodsTitle());
        orderStockTo.setOrderCode(childOrder.getOrderCode());
        orderStockTo.setOrderId(childOrder.getOrderId());
        orderStockTo.setSingleGoodsId(childOrder.getSingleGoodsId());
        orderStockTo.setSkuSetName(childOrder.getSkuSetName());
        orderStockTo.setUserMobile(childOrder.getMobile());
        orderStockTo.setUserId(childOrder.getUserId());
        orderStockTo.setGoodsId(childOrder.getGoodsId());
        orderStockTo.setShopId(childOrder.getShopId());
        productSingleGoodsService.orderOutStock(childOrder.getShopId(), orderStockTo);
    }

    /**
     * 内部调用创建子订单
     *
     * @param entity
     * @return
     */
    @Override
    public ChildOrder create(ChildOrder entity) {
        ValidatorUtils.validateEntity(entity);
        this.baseMapper.insert(entity);
        return entity;
    }

    @Override
    public ChildOrder modify(ChildOrder entity) {
        ValidatorUtils.validateEntity(entity);
        this.baseMapper.updateById(entity);
        return entity;
    }
    @Override
    public void updateChildOrder(ChildOrder entity) {
//        ValidatorUtils.validateEntity(entity);
        this.baseMapper.updateById(entity);
    }

    @Override
    public BigDecimal getObtainActivationTotalPrice(Long userId) {
        BigDecimal totalPrice = this.baseMapper.getObtainActivationTotalPrice(userId);
        return totalPrice;
    }

    @Override
    public void remove(Long id) {

    }

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        return null;
    }
}
